# 胡涵的python工厂
# 开发时间：2022/3/1 9:37
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
from tkinter import filedialog
import openpyxl
from openpyxl.styles import PatternFill,Font,Border,Side,Alignment
def main(ff):
    top=tk.Tk()
    screenwidth = top.winfo_screenwidth()
    screenheight = top.winfo_screenheight()
    alignstr = '%dx%d+%d+%d' % (600, 400, (screenwidth - 600) / 2, (screenheight - 400) / 2)
    top.geometry(alignstr)
    top.title('礼金管理系统')
    label= tk.Label(top, text='Excel文件：')
    excel=tk.StringVar()
    entry=tk.Entry(top,show=None,width=27,textvariable=excel)
    entry.place(x=200,y=40)
    filepath=''
    if len(ff)>1:
        filepath=ff
        entry.insert(0,ff)
    def file():
        global filepath
        root = tk.Tk()
        root.withdraw()
        file = filedialog.askopenfilename()
        entry.delete(0, 'end')
        entry.insert(0, file)
        filepath=file
    butt2 = tk.Button(top, text='浏览', command=file, width=7)
    butt2.place(x=420, y=35)
    def off1():
        global filepath
        try:
            if len(filepath) > 1:
                top.destroy()
                write(filepath)
        except:
            messagebox.showinfo('提示', '请查找文件')
    def off2():
        global filepath
        try:
            if len(filepath)>1:
                top.destroy()
                search(filepath)
        except:
            messagebox.showinfo('提示', '请查找文件')
    def off3():
        global filepath
        try:
            if len(filepath) > 1:
                top.destroy()
                SUM(filepath)
        except:
            messagebox.showinfo('提示', '请查找文件')
    butt1=tk.Button(top,text="登记",command=off1,width=15)
    butt2= tk.Button(top, text="查询", command=off2,width=15)
    butt3= tk.Button(top, text="统计", command=off3,width=15)
    butt1.place(x=230,y=100)
    butt2.place(x=230, y=180)
    butt3.place(x=230,y=260)
    label.place(x=120,y=40)
    top.mainloop()
def write(filepath):
    app = tk.Tk()
    screenwidth = app.winfo_screenwidth()
    screenheight = app.winfo_screenheight()
    alignstr = '%dx%d+%d+%d' % (600, 400, (screenwidth - 600) / 2, (screenheight - 400) / 2)
    app.geometry(alignstr)
    label1=tk.Label(app,text='人员类型：')
    label2=tk.Label(app,text='姓名：')
    label3=tk.Label(app,text='礼金(小写)：')
    label4=tk.Label(app,text='礼金(大写)：')
    label5=tk.Label(app,text='元')
    name=tk.StringVar()
    entry1=tk.Entry(app,show=None,width=27,textvariable=name)
    money=tk.StringVar()
    entry2=tk.Entry(app,show=None,width=27,textvariable=money)
    MONEY=tk.StringVar()
    entry3=tk.Entry(app,show=None,width=27,textvariable=MONEY)
    app.title('礼金登记系统')
    p_type=tk.StringVar()
    comb=ttk.Combobox(app,textvariable=p_type,width=25,height=2)
    comb["value"]=("男方亲人","女方亲人","男方朋友","女方朋友")
    label1.place(x=120,y=90)
    label2.place(x=120,y=140)
    label3.place(x=120, y=190)
    label4.place(x=120, y=240)
    label5.place(x=400,y=190)
    entry1.place(x=200,y=140)
    entry2.place(x=200,y=190)
    entry3.place(x=200,y=240)
    comb.place(x=200,y=90)
    def wr(*args,var):
        try:
            m=convert(var.get())
            entry3.delete(0, 'end')
            entry3.insert(0, m)
        except:
            pass
    money.trace('w',lambda *_,var=money: wr(*_,var=var))
    def getstr():
        s=0
        if comb.get() and entry1.get() and entry2.get() and entry3.get():
            lst=list(map(lambda x:x,[comb.get(),entry1.get(),entry3.get(),entry2.get()]))
            data=openpyxl.load_workbook(filepath)
            ws = data.get_sheet_by_name(data.get_sheet_names()[0])
            start=ws.max_column
            for i in range(2,6):
                ws.cell(row=i,column=start+1).value=lst[i-2]
                ws.cell(row=i,column=start+1).fill=PatternFill(fill_type='solid',fgColor='FF0000')
                if i!=5:
                    ws.cell(row=i,column=start+1).font=Font(size=22,bold=True)
                else:
                    ws.cell(row=i, column=start + 1).font = Font(size=18, bold=True)
                ws.cell(row=i,column=start+1).alignment=Alignment(vertical='center',horizontal='center',wrapText=True)
                ws.cell(row=i,column=start+1).border=Border(left=Side(border_style='thin',color='000000'),right=Side(border_style='thin',color='000000'),top=Side(border_style='thin',color='000000'),bottom=Side(border_style='thin',color='000000'))
            for j in range(5, ws.max_column+1):
                s+=int(ws.cell(row=5, column=j).value)
            ws.cell(row=5,column=4).value=s
            ws.cell(row=3,column=4).value=ws.max_column-4
            data.save(filepath)
            entry1.delete(0,'end')
            entry2.delete(0, 'end')
            entry3.delete(0, 'end')
        else:
            messagebox.showinfo('提示', '请输入完整的信息')
    butt=tk.Button(app,text='提交',width=10,command=getstr)
    butt.place(x=250,y=290)
    def handler():
        app.destroy()
        main(filepath)
    butt3=tk.Button(app,text='返回',width=10,command=handler)
    butt3.place(x=10, y=10)
    app.mainloop()
def search(filepath):
    app = tk.Tk()
    screenwidth = app.winfo_screenwidth()
    screenheight = app.winfo_screenheight()
    alignstr = '%dx%d+%d+%d' % (600, 400, (screenwidth - 600) / 2, (screenheight - 400) / 2)
    app.geometry(alignstr)
    app.title('礼金查询系统')
    label1 = tk.Label(app, text='姓名：')
    label2 = tk.Label(app, text='人员类型：')
    label3 = tk.Label(app, text='礼金(小写)：')
    label4 = tk.Label(app, text='礼金(大写)：')
    label5 = tk.Label(app, text='元')
    name = tk.StringVar()
    entry1 = tk.Entry(app, show=None, width=27, textvariable=name)
    entry2 = tk.Entry(app, show=None, width=27, textvariable=tk.StringVar())
    entry3 = tk.Entry(app, show=None, width=27, textvariable=tk.StringVar())
    entry4 = tk.Entry(app, show=None, width=27, textvariable=tk.StringVar())
    label1.place(x=120, y=90)
    label2.place(x=120, y=140)
    label3.place(x=120, y=190)
    label4.place(x=120, y=240)
    label5.place(x=400, y=190)
    entry1.place(x=200, y=90)
    entry2.place(x=200, y=140)
    entry3.place(x=200, y=190)
    entry4.place(x=200, y=240)
    def getstr():
        if entry1.get():
            lst=[]
            data = openpyxl.load_workbook(filepath)
            ws = data.get_sheet_by_name(data.get_sheet_names()[0])
            for i in range(5,ws.max_column+1):
                lst.append(ws.cell(row=3,column=i).value)
            if entry1.get() in lst:
                i=lst.index(entry1.get())+5
                entry2.delete(0, 'end')
                entry2.insert(0,ws.cell(row=2,column=i).value)
                entry3.delete(0, 'end')
                entry3.insert(0, ws.cell(row=5, column=i).value)
                entry4.delete(0, 'end')
                entry4.insert(0, ws.cell(row=4, column=i).value)
            else:
                entry2.delete(0, 'end')
                entry3.delete(0, 'end')
                entry4.delete(0, 'end')
                messagebox.showinfo('提示', '未找到此人')
        else:
            messagebox.showinfo('提示', '请输入姓名')
    butt = tk.Button(app, text='查询', width=10, command=getstr)
    butt.place(x=250, y=290)
    def handler():
        app.destroy()
        main(filepath)
    butt3=tk.Button(app,text='返回',width=10,command=handler)
    butt3.place(x=10, y=10)
    app.mainloop()
def convert(num):
    ch_num = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖', '拾']
    s_unit = ['拾', '佰', '仟']
    l=''
    num_l = []
    if len(num)==1:
        return ch_num[int(num)]+'元整'
    for i in num:
        num_l.append(int(i))
    for _ in range(len(num)):
        if len(num_l)==2:
            if  num_l[1]!=0 and num_l[0]!=0:
                l=l+ch_num[num_l[0]]+s_unit[len(num_l)-2]+ch_num[num_l[1]]+'元整'
            if num_l[1]!=0 and num_l[0]==0:
                l =l+ch_num[num_l[0]]+ch_num[num_l[1]]+'元整'
            if num_l[0]!=0 and num_l[1]==0:
                l=l+ch_num[num_l[0]] + s_unit[len(num_l)-2]+ '元整'
            if num_l[0]==0 and num_l[1]==0:
                l=l+'元整'
            break
        if len(num_l)==3:
            if num_l[0]!=0:
                l=l+ch_num[num_l[0]]+s_unit[len(num_l)-2]
                num_l.pop(0)
            elif num_l[1]!=0:
                l=l+ch_num[num_l[0]]
                num_l.pop(0)
            else:
                num_l.pop(0)
        if len(num_l)==4:
            l=l+ch_num[num_l[0]]+s_unit[len(num_l)-2]
            num_l.pop(0)
    return l
def SUM(filepath):
    bottom= tk.Tk()
    screenwidth = bottom.winfo_screenwidth()
    screenheight = bottom.winfo_screenheight()
    alignstr = '%dx%d+%d+%d' % (600, 400, (screenwidth - 600) / 2, (screenheight - 400) / 2)
    bottom.geometry(alignstr)
    bottom.title('礼金统计')
    data = openpyxl.load_workbook(filepath)
    ws = data.get_sheet_by_name(data.get_sheet_names()[0])
    label1=tk.Label(bottom,text='总人数:',font=('黑体',16))
    label2=tk.Label(bottom,text='总金额:',font=('黑体',16))
    label3= tk.Label(bottom, text=ws.cell(row=3,column=4).value,font=('黑体',16))
    label4= tk.Label(bottom, text=ws.cell(row=5,column=4).value,font=('黑体',16))
    label5 = tk.Label(bottom, text='人', font=('黑体', 16))
    label6 = tk.Label(bottom, text='元', font=('黑体', 16))
    label1.place(x=200, y=120)
    label2.place(x=200, y=180)
    label3.place(x=290, y=120)
    label4.place(x=290, y=180)
    label5.place(x=340, y=120)
    label6.place(x=370, y=180)
    def handler():
        bottom.destroy()
        main(filepath)
    butt3 = tk.Button(bottom, text='返回', width=10, command=handler)
    butt3.place(x=10, y=10)
    bottom.mainloop()
if __name__ == '__main__':
    main('')